using UnityEngine;

[CreateAssetMenu(menuName = "Zombie Character")]
public class ZombieCharacteristic : ScriptableObject
{
	public static int randomSkin;

	public Vector2 slide = default(Vector2);

	public float damageStrike = 1f;

	public int health = 10;

	public RuntimeAnimatorController animatorController;

	public string[] animationStart = new string[0];

	public Color[] colorsBody;

	public Color[] colorsLegs;

	public RandomSpriteName[] randomSprites = new RandomSpriteName[0];

	public bool damageSkin = true;

	public PhysicMaterialSprite[] physicsMaterialSprites = new PhysicMaterialSprite[0];

	public Sprite[] switchRenders = new Sprite[0];

	public AreaNameEnabled[] areaDamageDisable = new AreaNameEnabled[0];

	private SpriteRenderer[] renders = new SpriteRenderer[0];

	private Collider2D[] colliders;

	public virtual void OnDrawGizmosZombie(ZombieInHome zombie)
	{
		Gizmos.color = new Color(0f, 1f, 0.25f);
		Gizmos.DrawWireCube(zombie.gameObject.transform.position, new Vector3(slide.x * 2f, slide.y * 2f));
		Vector3 to = zombie.gameObject.transform.position + new Vector3(slide.x, slide.y);
		Gizmos.DrawLine(zombie.gameObject.transform.position, to);
	}

	public virtual void OnStartZombie(ZombieInHome zombie)
	{
		int num = 0;
		int num2 = 0;
		float num3 = 0f - slide.x + Mathf.Floor(100f * UnityEngine.Random.value) / 100f * (slide.x * 2f);
		float num4 = 0f - slide.y + Mathf.Floor(100f * UnityEngine.Random.value) / 100f * (slide.y * 2f);
		Transform transform = zombie.transform;
		Vector3 position = zombie.transform.position;
		float x = position.x + num3;
		Vector3 position2 = zombie.transform.position;
		float y = position2.y + num4;
		Vector3 position3 = zombie.transform.position;
		transform.position = new Vector3(x, y, position3.z);
		zombie.damageStrike = damageStrike;
		zombie.health = health;
		if (colorsBody.Length != 0)
		{
			zombie.colorBody = colorsBody[Random.Range(0, colorsBody.Length)];
		}
		if (colorsLegs.Length != 0)
		{
			zombie.colorLegs = colorsLegs[Random.Range(0, colorsLegs.Length)];
		}
		ChangeSprite(zombie.gameObject, zombie.colorBody, zombie.colorLegs, zombie.spritesOfBody, zombie.spritesOfLegs);
		zombie.animator.runtimeAnimatorController = animatorController;
		zombie.animator.Update(Time.deltaTime);
		if (animationStart.Length != 0)
		{
			zombie.animator.Play(animationStart[Random.Range(0, animationStart.Length)]);
		}
		for (num2 = 0; num2 < areaDamageDisable.Length; num2++)
		{
			for (num = 0; num < zombie.areas.Length; num++)
			{
				if (areaDamageDisable[num2].areaName == zombie.areas[num].areaName)
				{
					zombie.areas[num].enabled = areaDamageDisable[num2].enabled;
				}
			}
		}
		randomSkin++;
		if (randomSkin == 1)
		{
			randomSkin = UnityEngine.Random.Range(1, 400);
		}
		if (damageSkin)
		{
			RandomDamageSprites(zombie);
		}
	}

	public virtual EntityBehaviour GetBehaviour(ZombieInHome zombie)
	{
		return new ZombieBehaviour(zombie);
	}

	public virtual void ChangeSprite(GameObject parent, Color colorBody, Color colorLegs, SpriteRenderer[] body, SpriteRenderer[] legs)
	{
		int num = 0;
		int num2 = 0;
		renders = parent.GetComponentsInChildren<SpriteRenderer>(includeInactive: true);
		for (num = 0; num < body.Length; num++)
		{
			body[num].color = colorBody;
		}
		for (num = 0; num < legs.Length; num++)
		{
			legs[num].color = colorLegs;
		}
		for (num2 = 0; num2 < renders.Length; num2++)
		{
			for (num = 0; num < randomSprites.Length; num++)
			{
				if (!(randomSprites[num].replace == renders[num2].sprite))
				{
					continue;
				}
				renders[num2].sprite = randomSprites[num].sprites[randomSkin % randomSprites[num].sprites.Length];
				if (randomSprites[num].resetColor)
				{
					renders[num2].color = Color.white;
				}
				for (int i = 0; i < physicsMaterialSprites.Length; i++)
				{
					if (renders[num2].sprite == physicsMaterialSprites[i].sprite)
					{
						colliders = renders[num2].GetComponents<Collider2D>();
						for (int j = 0; j < colliders.Length; j++)
						{
							colliders[j].sharedMaterial = physicsMaterialSprites[i].material;
						}
						break;
					}
				}
			}
			for (num = 0; num < switchRenders.Length; num++)
			{
				if (renders[num2].sprite == switchRenders[num])
				{
					renders[num2].gameObject.SetActive(!renders[num2].gameObject.activeSelf);
					break;
				}
			}
		}
	}

	public void RandomDamageSprites(ZombieInHome zombie)
	{
		if (!DamageOfSprite.enabledComponents || zombie.spritesRigidbodies.Length == 0)
		{
			return;
		}
		Vector2[] array = new Vector2[2];
		int[] array2 = new int[array.Length];
		int num = 0;
		Collider2D component;
		for (num = 0; num < array.Length; num++)
		{
			array2[num] = UnityEngine.Random.Range(0, zombie.spritesRigidbodies.Length);
			component = zombie.spritesRigidbodies[array2[num]].GetComponent<Collider2D>();
			if (component != null && component.sharedMaterial == zombie.materialBody)
			{
				ref Vector2 reference = ref array[num];
				Vector3 min = component.bounds.min;
				float min2 = min.x * 100f;
				Vector3 max = component.bounds.max;
				reference.x = UnityEngine.Random.Range(min2, max.x * 100f) / 100f;
				ref Vector2 reference2 = ref array[num];
				Vector3 min3 = component.bounds.min;
				float min4 = min3.y * 100f;
				Vector3 max2 = component.bounds.max;
				reference2.y = UnityEngine.Random.Range(min4, max2.y * 100f) / 100f;
			}
			for (int i = 0; i < array2.Length; i++)
			{
				if (i != num && array2[num] == array2[i])
				{
					array[num] = Vector2.zero;
				}
			}
		}
		for (int j = 0; j < array.Length; j++)
		{
			if (array[j].x == 0f && array[j].y == 0f)
			{
				continue;
			}
			SpriteHoles.AddHole(zombie.spritesRigidbodies[array2[j]], array[j], zombie.materialHolesSkin);
			zombie.ShowMeat(zombie.spritesRigidbodies[array2[j]].transform);
			colliders = Physics2D.OverlapCircleAll(array[j], (float)SpriteHoles.GetTextureHole(zombie.materialHolesSkin).width / zombie.spritesRigidbodies[0].sprite.pixelsPerUnit);
			for (int k = 0; k < colliders.Length; k++)
			{
				for (num = 0; num < zombie.spritesRigidbodies.Length; num++)
				{
					if (zombie.spritesRigidbodies[num].gameObject == colliders[k].gameObject && (zombie.spritesRigidbodies[num].transform.parent == zombie.spritesRigidbodies[array2[j]].transform || zombie.spritesRigidbodies[array2[j]].transform.parent == zombie.spritesRigidbodies[num].transform))
					{
						SpriteHoles.AddHole(zombie.spritesRigidbodies[num], array[j], zombie.materialHolesSkin);
						zombie.ShowMeat(zombie.spritesRigidbodies[num].transform);
						break;
					}
				}
			}
		}
		component = null;
	}
}
